﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniStock.Client.UI.CommandService;
using MiniStock.Client.UI.Models;
using MiniStock.Client.UI.QueryService;

namespace MiniStock.Client.UI.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            var model = new CreateProductModel
            {
            };
            return View(model);
        }

        public ActionResult Get(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetProduct(id);
            return View(result);
        }

        public ActionResult List()
        {
            var service = new StockServiceFacadeClient();
            var result = service.ListProducts(null);
            return View(result);
        }

        public ActionResult Search(string name, string productNumber, string manufacturerId)
        {
            var service = new StockServiceFacadeClient();
            var queryToken = new QueryToken()
            {
                Query = new List<FilterElement>(),
                OrderBy = "Name",
                PageSize = 100,
                PageIndex = 0,
            };

            if (!String.IsNullOrEmpty(name))
                queryToken.Query.Add(
                    new FilterElement { FieldName = "Name", Operator = QueryOperator.Like, FieldValue = String.Format("'%{0}%'", name) });
            if (!String.IsNullOrEmpty(productNumber))
                queryToken.Query.Add(
                    new FilterElement { FieldName = "ProductNumber", Operator = QueryOperator.Like, FieldValue = String.Format("'%{0}%'", productNumber) });
            if (!String.IsNullOrEmpty(manufacturerId))
                queryToken.Query.Add(
                    new FilterElement { FieldName = "ManufacturerId", Operator = QueryOperator.Equals, FieldValue = String.Format("'{0}'", manufacturerId) });

            var result = service.ListProducts(queryToken);
            return Json(result.Select(i =>
                                          new
                                          {
                                              value = i.Id,
                                              label = i.Name,
                                          }).ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult ChangeProductNumber(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetProduct(id);
            return View(new ChangeProductNumberModel(result));
        }

        public ActionResult ChangeName(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetProduct(id);
            return View(new ChangeNameModel(result));
        }

        public ActionResult ChangeDescription(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetProduct(id);
            return View(new ChangeDescriptionModel(result));
        }

        public ActionResult ChangeManufacturer(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetProduct(id);
            return View(new ChangeManufacturerModel(result));
        }


        [HttpPost]
        public Guid CreateProduct(CreateProductModel model)
        {
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new CreateProductEvent()
                                                 {
                                                     Id = Guid.NewGuid(),
                                                     EventId = Guid.NewGuid(),
                                                     Name = model.Name,
                                                     ProductNumber = model.ProductNumber,
                                                 });
            return result;
        }

        [HttpPost]
        public Guid ChangeDescription(ChangeDescriptionModel model)
        {
            if (!this.ModelState.IsValid)
                return Guid.Empty;
            
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new ChangeProductDescriptionEvent()
            {
                Id = model.Id.Value,
                EventId = Guid.NewGuid(),
                Description = model.Description,
            });
            return result;
        }

        [HttpPost]
        public Guid ChangeManufacturer(ChangeManufacturerModel model)
        {
            if(!this.ModelState.IsValid)
                return Guid.Empty;

            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new ChangeProductManufacturerEvent()
            {
                Id = model.Id.Value,
                EventId = Guid.NewGuid(),
                ManufacturerId = model.ManufacturerId,
            });
            return result;
        }
    }
}
